package com.example.qa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qa.commonutils.R;
import com.example.qa.entity.ZhAnswer;
import com.example.qa.entity.ZhQuestion;
import com.example.qa.entity.vo.QuestionVo;
import com.example.qa.service.ZhAnswerService;
import com.example.qa.service.ZhQuestionService;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-02
 */
@CrossOrigin
@RestController
@RequestMapping("/qa/question")
public class ZhQuestionController {
    @Autowired
    private ZhQuestionService questionService;
    @Autowired
    private ZhAnswerService answerService;

    @GetMapping("/detail/{id}")
    @Cacheable(value = "questionDetail")
    public R getQuestionDetailById(@PathVariable String id) {
        ZhQuestion question = questionService.getById(id);
        return R.ok().data("info", question);
    }

    @PostMapping("/add")
    public R addQuestion(@RequestBody ZhQuestion question) {

        boolean save = questionService.save(question);
        return R.ok();
    }
    @PostMapping("/update")
    @CacheEvict(value = "questionDetail",key = "#question.id")
    public R updateQuestion(@RequestBody ZhQuestion question) {
         questionService.updateById(question);
        return R.ok();
    }

    @PostMapping("/pageQuestionCondition/{current}/{limit}")
    public R queryQuestionsPage(@PathVariable("current") int current, @PathVariable("limit") int limit, @RequestBody(required = false) QuestionVo questionVo) {
        Page<ZhQuestion> zhQuestionPage = new Page<>(current, limit);
        if (questionVo == null)
            questionService.page(zhQuestionPage, null);
        else {
            QueryWrapper<ZhQuestion> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(questionVo.getTitle()))
                wrapper.like("title", questionVo.getTitle());
            if (!StringUtils.isEmpty(questionVo.getUserName()))
                wrapper.eq("user_name", questionVo.getUserName());
            if (!StringUtils.isEmpty(questionVo.getBegin()))
                wrapper.ge("gmt_create", questionVo.getBegin());
            if (!StringUtils.isEmpty(questionVo.getEnd()))
                wrapper.le("gmt_create", questionVo.getBegin());
            wrapper.orderByDesc("gmt_create");
            questionService.page(zhQuestionPage, wrapper);
        }
        Long total = zhQuestionPage.getTotal();

        List<ZhQuestion> zhQuestionPageRecords = zhQuestionPage.getRecords();
        System.out.println(zhQuestionPageRecords);
        return R.ok().data("total", total).data("rows", zhQuestionPageRecords);
    }

    @DeleteMapping("/{questionId}")
    @CacheEvict(value = "questionDetail")
    public R deleteQuestion(@PathVariable String questionId) {
        boolean flag = questionService.removeById(questionId);
        if (flag) {
            QueryWrapper<ZhAnswer> wrapper = new QueryWrapper<>();
            wrapper.eq("question_id", questionId);
            boolean answerRemoveFlag = answerService.removeById(questionId);
            if (answerRemoveFlag)
                return R.ok();
            return R.error();
        }
        return R.error();
    }
}

